# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=bzip2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.0.6
pkgrel=6
pkgdesc="A high-quality data compression program (mingw-w64)"
arch=('any')
url="http://www.bzip.org"
license=("custom")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('strip' 'staticlibs')
source=("http://www.bzip.org/${pkgver}/bzip2-${pkgver}.tar.gz"
        "bzip2-1.0.5-slash.patch"
        "bzip2-1.0.4-bzip2recover.patch"
        "bzgrep-debian-1.0.5-6.all.patch"
        "bzip2-cygming-1.0.6.src.all.patch"
        "bzip2-buildsystem.all.patch"
        "bzip2-1.0.6-progress.all.patch"
       "bzip2-1.0.6-fix-heap-use-after-free-bzip2recover.patch")
sha256sums=('a2848f34fcd5d6cf47def00461fcb528a0484d8edef8208d6d2e2909dc61d9cd'
            '672216b20cf29438ffe43ebf38b9a648d9a0ac6fdc6be55bb4181d57ed5462be'
            '0585fb92a4b409404147a3f940ed2ca03b3eaed1ec4fb68ae6ad74db668bea83'
            'fe9212dcd0400d34269877b8de44b18767b9570048b85ed2a8d3c04e03940e9a'
            '7e67f77172b19f3e6c1f0875b1d3e9cb79211f8e1c752794ef9afd3704f928cf'
            'e519a50a4adaf05b18650d3e9d644badc66e80ca86063681ffe9a2c2226319d0'
            'f93e6b50082a8e880ee8436c7ec6a65a8f01e9282436af77f95bb259b1c7f7f7'
            '7b96cf49248ff3eb24f9e552861b91848f45526a2ce07219bef925e663afe313')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
# remove files that are created by patches.
  rm -rf README.CYGMING | true
  rm -rf aclocal.m4 | true
  rm -rf configure.ac | true
  rm -rf libbz2.def.in | true
  rm -rf bzip2.pc.in | true
  rm -rf Makefile.in | true

  patch -p1 -i "$srcdir/"bzip2-1.0.4-bzip2recover.patch
  patch -p1 -i "$srcdir/"bzip2-1.0.5-slash.patch
  patch -p1 -i "$srcdir/"bzgrep-debian-1.0.5-6.all.patch
  patch -p1 -i "$srcdir/"bzip2-cygming-1.0.6.src.all.patch
  patch -p1 -i "$srcdir/"bzip2-buildsystem.all.patch
  patch -p1 -i "$srcdir/"bzip2-1.0.6-progress.all.patch
  patch -p1 -i "$srcdir/bzip2-1.0.6-fix-heap-use-after-free-bzip2recover.patch"
  autoreconf -fi
}

build() {
  mkdir -p "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-shared

  make all-dll-shared
}

check() {
  cd "${srcdir}/build-${CARCH}"
  make -k check
}

package() {
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR="${pkgdir}" install
  # rm "${pkgdir}${MINGW_PREFIX}/bin/bz"{diff,grep,more}
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
